﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TrafficTrouble.Components
{
    public class Grid
    {
        public string Name { get; private set; }

        public int Width { get; private set; }
        public int Height { get; private set; }

        public Car[] Cars { get; private set; }

        public Grid(string name, int width, int height, params Car[] cars)
        {
            this.Name = name;

            this.Width = width;
            this.Height = height;

            this.Cars = cars;
        }

        public Car GetCarAt(int x, int y)
        {
            foreach (Car car in this.Cars)
                if (car.ContainsLocation(x, y))
                    return car;
            return null;
        }

        public bool IsEmpty(int x, int y)
        {
            return this.GetCarAt(x, y) == null;
        }

        public bool ContainsCar(int x, int y)
        {
            return !this.IsEmpty(x, y);
        }

        public void AddCar(Car car)
        {
            List<Car> cars = new List<Car>(this.Cars);
            cars.Add(car);
            this.Cars = cars.ToArray();
        }

        public override string ToString()
        {
            //string grid = "+" + new string('-', this.Width) + "+\n";
            //for (int y = 0; y < this.Height; y++)
            //    grid += "|" + new string('.', this.Width) + "|\n";
            //grid += "+" + new string('-', this.Width) + "+\n";
            StringBuilder grid = new StringBuilder("");
            for (int y = 0; y < this.Height; y++)
                grid = grid.AppendLine(new string('.', this.Width));

            char currentPlayerCarChar = 'A';
            char currentCarChar = 'a';
            foreach (Car car in this.Cars)
            {
                foreach (Point carLoc in car.GetContainingLocations())
                    grid = grid.Replace('.', car.PlayerCar ? currentPlayerCarChar : currentCarChar, 
                        carLoc.X + this.Width * carLoc.Y,
                        1);
                if (car.PlayerCar) currentPlayerCarChar++;
                else currentCarChar++;
            }

            grid = grid.Insert(0, "+" + new string('-', this.Width) + "+\n");
            for (int y = 0; y < this.Height; y++)
            {
                grid = grid.Insert((2 + this.Width) * (y + 1), "|");
                grid = grid.Insert((2 + this.Width) * (y + 1) + 1 + this.Width, "|");
            }
           grid = grid.Append("+" + new string('-', this.Width) + "+");

            return grid.ToString();
        }

        public static bool operator ==(Grid g1, Grid g2)
        {
            return g1.ToString() == g2.ToString();
        }
        public static bool operator !=(Grid g1, Grid g2)
        {
            return !(g1 == g2);
        }
        public override bool Equals(object obj)
        {
            if (!(obj is Grid)) return false;
            return this == (Grid)obj;
        }
    }
}
